package ui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MainGUI extends JFrame implements ActionListener {
	
	private JButton _insertData = new JButton("Neuer Datensatz");
	private JButton _credit = new JButton("Kredit vergeben");
	private JButton _insurance = new JButton("Versicherung abschließen");
	
	private JTable _table;
	private DefaultTableModel _defaultTableModel;
	
	private JComboBox _tableList;
	
	private JPanel _panelButtons = new JPanel(new GridLayout(1,3));
	
	private Vector _currentTables;
	private ResultSet _currentResultSet;
	
	private static MainGUI _instance = null;

	/**
	 * Erzeugt neues MainGUI
	 */
	private MainGUI() {
		super("Aufgabe 2");
		
		//ComboBox
		String[] tables = { "Immobilie", 
							"Privatperson", 
							"Unternehmen", 
							"Bank", 
							"Versicherungsunternehmen", 
							"Darlehen",
							"Darlehensnehmer",
							"Versichert",
							"Protokoll"
							};
		
		_tableList = new JComboBox(tables);
		_tableList.setSelectedIndex(0);

		//Frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		this.setSize(800, 400);		

		//Buttons
		_insertData.setActionCommand("insert");
		_credit.setActionCommand("credit");
		_insurance.setActionCommand("insurance");
		_tableList.setActionCommand("select_table");
		
		_insertData.addActionListener(this);
		_credit.addActionListener(this);
		_insurance.addActionListener(this);
		_tableList.addActionListener(this);
		
		_panelButtons.add(_tableList);
		_panelButtons.add(_insertData);
		_panelButtons.add(_credit);
//		_panelButtons.add(_insurance);

		//Table
		_table = new JTable();
		_defaultTableModel = (DefaultTableModel) _table.getModel();
		_table.setEnabled(false);

		//Componenten zum Frame hinzufügen
		this.add(new JScrollPane(_table), BorderLayout.CENTER);
		this.add(this._panelButtons, BorderLayout.SOUTH);
		
		ResultSet data;
		
		_tableList.setSelectedItem("Immobilie");
	
		
	}
	
	
	/**
	 * Actionen zu den jeweilig gedrueckten Buttons
	 */
	public void actionPerformed(ActionEvent ae) {
		
		// Datensatz anlegen
		if (ae.getActionCommand().equals("insert")) {
			
		}
		
		else if (ae.getActionCommand().equals("credit")) {

		}
		
		// Tabelle auswählen
		else if (ae.getActionCommand().equals("select_table")) {

			ResultSet data;
			
			if (_tableList.getSelectedItem().toString().equals("Privatperson")) {
				
				_currentTables = new Vector() {{
					add("darlehensnehmer");
					add("privatperson");
					}
				};
				
				
				
				data = null;
				_currentResultSet = data;
			}

		}	
	}
	
	/**
	 * Aktualisiert die Tabelle
	 * @param rs
	 * @throws SQLException
	 */
	public void updateTableModel(ResultSet rs) throws SQLException {
		
		Vector resultTable = new Vector();
		Vector columns = null;
		
		while (rs.next()) {
			
			Vector row = new Vector();
			
			for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {

				row.add(rs.getString(rs.getMetaData().getColumnName(i)));
			}
			
			resultTable.add(row);
		}

		
		_defaultTableModel.setDataVector(resultTable, columns);
		_defaultTableModel.setColumnCount(columns.size());
	}
	
	/**
	 * Liefert Instanz des Managers
	 * 
	 * @return DataManager
	 */
	public static MainGUI getInstance() {
		if (_instance == null) {
			_instance = new MainGUI();
		}
		return _instance;
	}

}
